package com.cmcc.linlaker.utils;

import android.content.Context;
import android.content.SharedPreferences;

public class SPHelper {

    private static SPHelper helper;
    private SharedPreferences settings = null;
    private Context ctx;
    private SharedPreferences.Editor editor;
    private String sPrefsName;

    private static final String UID = "uid";
    private static final String CLIENT_ID = "client_id";
    private static final String scaleRate_W = "scaleRate_W";
    private static final String scaleRate = "scaleRate";
    private static final String USERNAME = "username";
    private static final String PASSWORD = "password";
    private static final String AUTO_LOGIN = "auto_login";
    private static final String AUTH_TOKEN = "auth_token";
    private static final String NICKNAME = "nickname";
    private static final String CHANNEL_ID = "channel_id";

    public static void init(Context context) {
        helper = new SPHelper(context);
    }

    public static SPHelper getInstance() {
        if (helper == null) {
            throw new NullPointerException(
                    "NOT INIT sphelper,please call init in app first");
        }
        return helper;
    }

    private SPHelper(Context context) {
        this.ctx = context;
        sPrefsName = ctx.getPackageName();
        this.settings = ctx.getSharedPreferences(sPrefsName,
                Context.MODE_PRIVATE);
        this.editor = settings.edit();
    }

    public static String getChannelId() {
        return getInstance().settings.getString(CHANNEL_ID, "");
    }

    public static void setChannelId(String channelId) {
        getInstance().editor.putString(CHANNEL_ID, channelId);
        getInstance().editor.commit();
    }

    public static String getUid() {
        return getInstance().settings.getString(UID, "");
    }

    public static void setUid(String uid) {
        getInstance().editor.putString(UID, uid);
        getInstance().editor.commit();
    }

    public static double getScaleRate_W() {
        return getInstance().settings.getFloat(scaleRate_W, 0.0f);
    }

    public static void setScaleRate_W(float scaleRateW) {
        getInstance().editor.putFloat(scaleRate_W, scaleRateW);
        getInstance().editor.commit();
    }

    public static double getScaleRate() {
        return getInstance().settings.getFloat(scaleRate, 0.0f);
    }

    public static void setScaleRate(float scaleRatef) {
        getInstance().editor.putFloat(scaleRate, scaleRatef);
        getInstance().editor.commit();
    }

    public static String getUsername() {
        return getInstance().settings.getString(USERNAME, "");
    }

    public static void setUsername(String username) {
        getInstance().editor.putString(USERNAME, username);
        getInstance().editor.commit();
    }

    public static String getPassword() {
        return getInstance().settings.getString(PASSWORD, "");
    }

    public static void setPassword(String password) {
        getInstance().editor.putString(PASSWORD, password);
        getInstance().editor.commit();
    }

    public static String getClientId() {
        return getInstance().settings.getString(CLIENT_ID, "");
    }

    public static void setClientId(String cid) {
        getInstance().editor.putString(CLIENT_ID, cid);
        getInstance().editor.commit();
    }

    public static boolean getAutoLogin() {
        return getInstance().settings.getBoolean(AUTO_LOGIN, false);
    }

    public static void setAutoLogin(boolean auto_login) {
        getInstance().editor.putBoolean(AUTO_LOGIN, auto_login);
        getInstance().editor.commit();
    }

    public static String getAuthToken() {
        return getInstance().settings.getString(AUTH_TOKEN, "");
    }

    public static void setAuthToken(String authToken) {
        getInstance().editor.putString(AUTH_TOKEN, authToken);
        getInstance().editor.commit();
    }

    public static String getNickname() {
        return getInstance().settings.getString(NICKNAME, "");
    }

    public static void setNickname(String nickname) {
        getInstance().editor.putString(NICKNAME, nickname);
        getInstance().editor.commit();
    }
}
